<?php

class Submit
{
    function view($form)
    {
        // Check onSubmit
        if(isset($_POST['submit']))
        {
            // Actions
            $form['actions'];
        }
        else
        {
            $method = empty($form['method']) ? '' : $form['method'];
            $action = empty($form['action']) ? '' : $form['action'];
            
            // Setup Default Buttons
            if(empty($form['buttons']))
            {
                $form['buttons'] =  array
                                    (
                                        'submit' => array('submit'),
                                    );
            }
            
            // Show Form
            echo $this->Form($method, $action, $form['fields'], $form['buttons']);
        }
    }
    
    function onSubmit()
    {
        // Actions
    }
    
    function Form($m='', $a='', $fields, $buttons)
    {
        $method = (strtolower($m) == 'post') ? ' method="POST"'   : '';
        $action = (!empty($a))               ? ' action="'.$a.'"' : '';
        
        // Form
        $form = '<form'.$method.''.$action.'>';
        
        foreach($fields as $k => $v)
        {
            $type        = (empty($v[1])) ? 'text' : strtolower($v[1]);
            $value       = (!empty($v[2])) ? ' value = "'.$v[2].'"' : '';
            $placeholder = (!empty($v[3])) ? ' placeholder="'.$v[3].'"' : '';
            
            $name = strtolower($k);
            
            if($type == 'textarea')
            {
                $value       = (!empty($v[2])) ? $v[2] : '';
                $form .= ucfirst($name).'<br /><textarea id="'.$name.'" cols="45" rows="5" name="'.$name.'" '.$placeholder.' '.$v[0].'>'.$value.'</textarea>
            <br />';
            }
            elseif($type == 'dropdown')
            {
                $form .= ucfirst($name).'<br />
                <select name="'.$name.'" '.$placeholder.' '.$v[0].'>';
                    foreach($v[2] as $k2 => $v2)
                    {
                        $val = ($v[0] == 'k') ? $k2 : $v2;
                        $slect = ($v[3] == $val) ? ' selected' : '';
                        
                        $form .= '<option value="'.$val.'"'.$slect.'>'.$v2.'</option>';
                    }
                $form .= '</select><br />';
            }
            else
            {
                $value       = (!empty($v[2])) ? ' value = "'.$v[2].'"' : '';
                $form .= ucfirst($name).' <input type="'.$type.'" id="'.$name.'" name="'.$name.'"'.$value.$placeholder.' '.$v[0].'>
            <br />';
            }
            
            $form .= '<br />';
        }

        foreach($buttons as $k => $v)
        {
            $type1 = (empty($v[0])) ? 'submit' : $v[0];
            $text1 = (empty($v[1])) ? ' value="'.$v[0].'"' : ' value="'.$v[1].'"';
            
            $form .= ' <input type="'.$type1.'" name="'.$k.'"'.$text1.' /> ';
        }
        
        return $form.'</form>';
    }
}

/*
$form = array(
    'method' => 'post',

    'fields' => array
    (
    //  name   => array(options, type, value, placeholder)
    //  name   => array(options) Default Type = text, Default rest: empty
        'user' => array('required'),
        'pass' => array('required', 'password'),
    )
);

$submit = new Submit;

$submit->view($form);


$form2 = array(
    'method'  => 'post',
    'actions' => array(
        'action_type' => 'values'),
    'fields'  => array(
        'user' => array('required'),
        'pass' => array('required', 'password'))
);
*/